package com.wang.jmonkey.common.result;

import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.http.HttpStatus;

/**
 * @Description service接口返回数据
 * @Author HeJiawang
 * @Date 2021/5/8 11:17
 */
@Data
@Accessors(chain = true)
public class ServiceResult<T> {

    /**
     * 请求状态码
     */
    private Integer code;

    /**
     * 请求是否成功
     */
    private boolean success;

    /**
     * 请求结果信息
     */
    private String msg;

    /**
     * 请求结果数据
     */
    private T data;

    public ServiceResult(T data) {
        this.code = HttpStatus.SC_OK;
        this.success = true;
        this.data = data;
    }

    public ServiceResult() {
        this.code = HttpStatus.SC_OK;
        this.success = true;
    }

    public ServiceResult(Integer code, boolean success,String msg) {
        this.code = code;
        this.success = success;
        this.msg = msg;
    }

    public ServiceResult<T> error(String message) {
        this.code = HttpStatus.SC_INTERNAL_SERVER_ERROR;;
        this.success = false;
        this.msg = message;
        return this;
    }

    public HttpResult<T> convert() {
        return new HttpResult<>(code, success, data, msg);
    }
}
